<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="公司名称:">
              <el-input
                clearable
                v-model="where.company"
                placeholder="请输入公司名称"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="收款通道:">
              <el-select
                :placeholder="'请选择收款通道'"
                v-model="where.pay_way_id"
              >
                <el-option
                  v-for="item in pay_way_idAll"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:paymentaccount:add')"
            >添加
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:paymentaccount:edit')"
            >修改
          </el-link>
        </template>
        <template v-slot:params="{ row }">
          <div class="params-cell">
            {{ row.params }}
          </div>
          <!-- <div class="params-cell">
            {{ row.params ? formatParamsAppid(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsPartner(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsMchid(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsMchidRsaprivatekey(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsMchidPay_url(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsMchidRsapublickey(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsMchidSeller_email(row.params) : '' }}
          </div>
          <div class="params-cell">
            {{ row.params ? formatParamsMchidKeyZFB(row.params) : '' }}
          </div> -->
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <PaymentAccountEdit
      v-if="showEdit"
      :data="current"
      :visible.sync="showEdit"
      @done="reload"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import PaymentAccountEdit from './paymentaccount-edit'

export default {
  name: 'ExamPaymentAccount',
  components: { PaymentAccountEdit },
  computed: {
    ...mapGetters(['permission'])
  },
  created() {
    // this.$router.go(0)
    this.getAll()
  },
  data() {
    return {
      // 表格数据接口
      url: '/paymentaccount/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center'
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'company',
          label: '收款账号公司名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'pay_way_name',
          label: '收款通道',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'params',
          label: '收款参数',
          align: 'center',
          width: 500,
          slot: 'params'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      pay_way_idAll: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    }
  },
  methods: {
    getAll() {
      this.$http.post('/payway/getAll').then((res) => {
        if (res.data.code === 0) {
          this.pay_way_idAll = res.data.data
        }
      })
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where })
    },
    /* 重置搜索 */
    reset() {
      this.where = {}
      this.reload()
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row
      this.showEdit = true
    },

    formatParamsAppid(params) {
      const dataObj = JSON.parse(params)
      // 支付宝
      let appid = dataObj.appid ? '应用id:' + dataObj.appid : ''
      return `${appid}`
    },
    formatParamsPartner(params) {
      const dataObj = JSON.parse(params)
      // 支付宝
      let partner = dataObj.partner ? '合作伙伴身份PID:' + dataObj.partner : ''
      return `${partner}`
    },
    formatParamsMchid(params) {
      const dataObj = JSON.parse(params)
      // 微信
      let mchid = dataObj.mchid ? '商户号:' + dataObj.mchid : ''
      return `${mchid}`
    },

    formatParamsMchidRsaprivatekey(params) {
      const dataObj = JSON.parse(params)
      // 支付宝
      let rsaprivatekey = dataObj.rsaprivatekey
        ? '商户密钥:' + dataObj.rsaprivatekey
        : '商户密钥:' + dataObj.key
      return `${rsaprivatekey}`
    },

    formatParamsMchidPay_url(params) {
      const dataObj = JSON.parse(params)
      // 微信
      let pay_url = dataObj.pay_url ? '支付域名:' + dataObj.pay_url : ''
      return `${pay_url}`
    },
    formatParamsMchidRsapublickey(params) {
      const dataObj = JSON.parse(params)
      // 支付宝
      let rsapublickey = dataObj.rsapublickey
        ? '支付宝公钥:' + dataObj.rsapublickey
        : ''
      return `${rsapublickey}`
    },

    formatParamsMchidSeller_email(params) {
      const dataObj = JSON.parse(params)
      // 支付宝
      let seller_email = dataObj.seller_email
        ? '收款帐户邮箱:' + dataObj.seller_email
        : ''
      return `${seller_email}`
    },
    formatParamsMchidKeyZFB(params) {
      const dataObj = JSON.parse(params)
      // 支付宝
      let keyZFB =
        dataObj.key && dataObj.rsaprivatekey ? 'MD5密钥:' + dataObj.key : ''
      return `${keyZFB}`
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true })
      this.$http
        .post('/paymentaccount/delete', { id: row.id })
        .then((res) => {
          loading.close()
          if (res.data.code === 0) {
            this.$message.success(res.data.msg)
            this.reload()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((e) => {
          loading.close()
          this.$message.error(e.message)
        })
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return
      }
      this.$confirm('确定要删除选中的收款账户吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true })
          this.$http
            .post('/paymentaccount/delete', {
              id: this.selection.map((d) => d.id)
            })
            .then((res) => {
              loading.close()
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                this.reload()
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              loading.close()
              this.$message.error(e.message)
            })
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped>
.params-cell {
  height: auto;
  max-height: 150px;
  overflow: auto;
  text-overflow: ellipsis;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 3;
}
</style>
